package zhxt.helmet.dao;

import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import zhxt.helmet.pojo.User;

import java.util.List;

@Repository
public interface UserDao {
    // 根据登录名和密码查询管理员
@Select("select * from user where id = #{id} and password=#{password}")
@Results({
        //@Result(column = "name", property = "name"),
        @Result(column = "province_id", property = "provinceId"),
        @Result(column = "city_id", property = "cityId"),
        @Result(column = "district_id", property = "districtId"),
        @Result(column = "block_id", property = "blockId"),
        @Result(column = "province_name", property = "provinceName"),
        @Result(column = "city_name", property = "cityName"),
        @Result(column = "district_name", property = "districtName"),
        @Result(column = "block_name", property = "blockName")
})
    User selectByIdAndPassword(User user);

    //查全部用户
    @Select("select * from user ")
    @Results({
            //@Result(column = "name", property = "name"),
            @Result(column = "province_id", property = "provinceId"),
            @Result(column = "city_id", property = "cityId"),
            @Result(column = "district_id", property = "districtId"),
            @Result(column = "block_id", property = "blockId"),
            @Result(column = "province_name", property = "provinceName"),
            @Result(column = "city_name", property = "cityName"),
            @Result(column = "district_name", property = "districtName"),
            @Result(column = "block_name", property = "blockName")
    })
    List<User> selectAllUsers();

    // 添加用户
    @Insert("insert into user(name, password , func, province_id, province_name, city_id, city_name, district_id, district_name, block_id, block_name) "
            + "values(#{name},#{password},#{func},#{provinceId},#{provinceName},#{cityId},#{cityName},#{districtId},#{districtName},#{blockId},#{blockName})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void add(User ui);

    //更新用户
    @Update("update user  set " +
            "name=#{name}, " +
            "password=#{password}, " +
            "func=#{func}, " +
            "province_id=#{provinceId}, " +
            "province_name=#{provinceName}, " +
            "city_id=#{cityId}, " +
            "city_name=#{cityName}, " +
            "district_id=#{districtId}, " +
            "district_name=#{districtName}, " +
            "block_id=#{blockId}, " +
            "block_name=#{blockName} " +
            "where id=#{id}")
    void update(User ui);

    //删除用户
    @Delete("DELETE FROM user WHERE id = #{id}")
    void delete(User ui);
}
